/*
 * GUICadastroOperadores.java
 *
 * Created on 28/05/2011, 23:07:13
 */

package gui;

import dao.DAOCategoria;
import dao.DAOFuncao;
import dao.DAOOperador;
import dao.ResultSetComboBox;
import javax.swing.JOptionPane;
import model.KeyValue;
import model.Operador;

/**
 * Interface utilizada para cadastrar novos operadores e alterar operadores existentes
 * @author Janio Luiz Antunes
 * @author Marcel Vinicius Kohls
 * @see http://code.google.com/p/sysdesk-java/
 *
 */
public class GUICadastroOperadores extends javax.swing.JFrame {

    Operador operador;
    DAOOperador operadorDAO = new DAOOperador();
    Operador operadorOrigem = new Operador();
    DAOCategoria categoriaDAO = new DAOCategoria();
    DAOFuncao funcaoDAO = new DAOFuncao();

    /** 
     * Inicializa form de cadastro/alteracao de Operadores GUICadastroOperadores
     */
    public GUICadastroOperadores() {
        initComponents();

        // Carrega os dados na combobox
        try{
            jcbOpCategoria.setModel(new ResultSetComboBox(categoriaDAO.getListaCategorias(""), "cat_descricao", "cat_codigo", "").getModel() );
        } catch (Exception e) {
            System.out.println("erro: "+e.getMessage());
        }

        try{
            jcbOpFuncao.setModel(new ResultSetComboBox(funcaoDAO.getListaFuncoes(), "fun_nome", "fun_codigo", "").getModel() );
        } catch (Exception e) {
            System.out.println("erro: "+e.getMessage());
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel5 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jcbOpFuncao = new javax.swing.JComboBox();
        jtfOpCodigo = new javax.swing.JTextField();
        jtfOpNome = new javax.swing.JTextField();
        jtfTelefone = new javax.swing.JTextField();
        jtfOpEmail = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jcbOpCategoria = new javax.swing.JComboBox();
        jbOpSalvar = new javax.swing.JButton();
        jtCancelar = new javax.swing.JToggleButton();
        jSeparator1 = new javax.swing.JSeparator();

        jLabel5.setText("jLabel5");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Sysdesk - Cadastro de Operadores");
        setResizable(false);

        jLabel1.setText("Código");

        jLabel2.setText("Nome");

        jLabel3.setText("Telefone");

        jLabel4.setText("Email");

        jLabel6.setText("Função");

        jtfOpCodigo.setEditable(false);

        jLabel7.setText("Categoria");

        jcbOpCategoria.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione a categoria", "Hardware - Desktop", "Hardware - Notebook", "Hardware - Impressora", "Hardware - Monitor", "Software - Windows", "Software - Linux", "Software - MacOS", "Banco de dados", "Programação", "Atendimento Telefônico" }));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(94, 94, 94)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jtfOpCodigo, javax.swing.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)
                            .addContainerGap(330, Short.MAX_VALUE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jtfOpNome, javax.swing.GroupLayout.PREFERRED_SIZE, 335, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap())
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jcbOpCategoria, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jcbOpFuncao, javax.swing.GroupLayout.Alignment.LEADING, 0, 167, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jtfOpEmail, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtfTelefone, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtfOpCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtfOpNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jtfTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jtfOpEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcbOpFuncao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcbOpCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addGap(52, 52, 52))
        );

        jbOpSalvar.setText("Salvar");
        jbOpSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbOpSalvarActionPerformed(evt);
            }
        });

        jtCancelar.setText("Cancelar");
        jtCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(354, Short.MAX_VALUE)
                        .addComponent(jtCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbOpSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 528, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbOpSalvar)
                    .addComponent(jtCancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     *
     * Fecha esta janela
     */
    private void jtCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtCancelarActionPerformed
        this.dispose();
}//GEN-LAST:event_jtCancelarActionPerformed

    /**
     *
     * Salva o cadastro do operador no banco
     */
    private void jbOpSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbOpSalvarActionPerformed
        try{
            operadorOrigem.setCodCategoria((Integer) (((KeyValue) jcbOpCategoria.getSelectedItem()).getValue()));
            operadorOrigem.setNome(jtfOpNome.getText());
            operadorOrigem.setCodFuncao((Integer) (((KeyValue) jcbOpFuncao.getSelectedItem()).getValue()));
            operadorOrigem.setTelefone(jtfTelefone.getText());
            operadorOrigem.setEmail(jtfOpEmail.getText());


            if  (operadorDAO.salvar(operadorOrigem) == true) {
                JOptionPane.showMessageDialog(null, "Operador salvo com sucesso!");
                this.dispose();
            }
        } catch (Exception e) {
            if (e.getMessage()==null){
                JOptionPane.showMessageDialog(null, "Erro: Obrigatório selecionar função e categoria");
            }else{
                JOptionPane.showMessageDialog(null, "Erro: "+e.getMessage());
            }
        }
}//GEN-LAST:event_jbOpSalvarActionPerformed
       
    /**
     * Passa o código selecionado para a função Alterar
     * @param pCodigoOperador
     */
    public void setCodigoSelecionado(int pCodigoOperador){
        if (pCodigoOperador != 0) {
            try{
                System.out.println("setCodigoOperador: Codigo="+Integer.toString(pCodigoOperador));
                operadorOrigem = operadorDAO.getOperadorPorCodigo(pCodigoOperador);
            } catch (Exception e) {
                System.out.println("erro: setCodigoSelecionado: "+e.getMessage());
            }
        }

        jtfOpCodigo.setText(Integer.toString(operadorOrigem.getCodigo()));
        jtfOpNome.setText(operadorOrigem.getNome());
        jtfTelefone.setText(operadorOrigem .getTelefone());
        jtfOpEmail.setText(operadorOrigem.getEmail());

        // Carrega os dados na combobox
        try{//jcbCliente.setModel(new ResultSetComboBox(clienteDAO.getListaClientes(-1), "cli_nome", "cli_codigo", usuarioOrigem.getUsu_cli_codigo()).getModel());
            jcbOpFuncao.setModel(new ResultSetComboBox(funcaoDAO.getListaFuncoes(), "fun_nome", "fun_codigo", operadorOrigem.getCodFuncao()).getModel());
            jcbOpCategoria.setModel(new ResultSetComboBox(categoriaDAO.getListaCategorias(""), "cat_descricao", "cat_codigo", operadorOrigem.getCodCategoria()).getModel());
        } catch (Exception e) {
            System.out.println("erro: "+e.getMessage());
        }

    }

    /**
     * 
     * @param args
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUICadastroOperadores().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton jbOpSalvar;
    private javax.swing.JComboBox jcbOpCategoria;
    private javax.swing.JComboBox jcbOpFuncao;
    private javax.swing.JToggleButton jtCancelar;
    private javax.swing.JTextField jtfOpCodigo;
    private javax.swing.JTextField jtfOpEmail;
    private javax.swing.JTextField jtfOpNome;
    private javax.swing.JTextField jtfTelefone;
    // End of variables declaration//GEN-END:variables

}
